package com.dhl.single;

/**
 * @title: 双重检查
 * @Author danghailiang
 * @Date: 2022/7/26 21:02
 * @Version 1.0
 */
public class 双重检查 {
    public static void main(String[] args) {
        new Thread(() -> {
            SingleTwoCheck instance1 = null;
            instance1 = SingleTwoCheck.getInstance();
            System.out.println(instance1);
        }).start();

        new Thread(() -> {
            SingleTwoCheck instance = null;
            instance = SingleTwoCheck.getInstance();
            System.out.println(instance);
        }).start();
    }
}
class SingleTwoCheck{
    /**
     * 构造器私有化
     */
    private SingleTwoCheck() {

    }
    /**
     * 创建静态变量
     */
    private static SingleTwoCheck instance;
    /**
     * 返回对象
     */
    public static synchronized SingleTwoCheck getInstance() {
        if(instance == null ){
            synchronized (SingleTwoCheck.class){
                if(instance == null ){
                    instance = new SingleTwoCheck();
                }
            }
        }
        return instance;
    }
}
